package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.wjjBO.WXCouponExchangeBO;
import com.cskaoyan.bean.bo.wjjBO.WXCouponReceiveBO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.marketBean.MarketCouponUser;
import com.cskaoyan.bean.marketBean.MarketUser;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.CouponService_yjc;
import com.cskaoyan.service.UserService_yjc;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("wx/coupon")
public class YJCCouponController {

    @Autowired
    CouponService_yjc couponService_yjc;

    @RequestMapping("list")
    public BaseRespVo<YJCUserVo> list() {
        PageInfo pageInfo = couponService_yjc.queryAllCoupons();
        long total = pageInfo.getTotal();
        List list = pageInfo.getList();
        YJCUserVo yjcUserVo = new YJCUserVo((int) total, 1, 10, 1, list);
        return BaseRespVo.okyjc(yjcUserVo);
    }

    @RequestMapping("mylist")
    public BaseRespVo<YJCUserVo> myList() {
        PageInfo pageInfo = couponService_yjc.queryMyCoupons();
        long total = pageInfo.getTotal();
        List list = pageInfo.getList();
        YJCUserVo yjcUserVo = new YJCUserVo((int) total, 1, 10, 1, list);
        return BaseRespVo.okyjc(yjcUserVo);
    }

    @RequestMapping("selectlist")
    public BaseRespVo<YJCUserVo> selectlist() {
        PageInfo pageInfo = couponService_yjc.selectCoupon();
        long total = pageInfo.getTotal();
        List list = pageInfo.getList();
        YJCUserVo yjcUserVo = new YJCUserVo((int) total, 1, 10, 1, list);
        return BaseRespVo.okyjc(yjcUserVo);
    }

    @RequestMapping("receive")
    public BaseRespVo couponReceive(@RequestBody WXCouponReceiveBO couponReceiveBO) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();

        couponReceiveBO.setUserId(userId);
        int code = couponService_yjc.receiveCoupon(couponReceiveBO);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        if (code == 200) {
            baseRespVo.setErrmsg("领取成功");
            baseRespVo.setErrno(0);
        } else if (code == 401) {
            baseRespVo.setErrmsg("领取上限");
            baseRespVo.setErrno(740);
        }
        return baseRespVo;
    }

    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody WXCouponExchangeBO wxCouponExchangeBO) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();

        wxCouponExchangeBO.setUserId(userId);
        int code = couponService_yjc.exchangeCoupon(wxCouponExchangeBO);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        if (code == 200) {
            baseRespVo.setErrmsg("成功");
            baseRespVo.setErrno(0);
        } else if (code == 401) {
            baseRespVo.setErrmsg("领取上限");
            baseRespVo.setErrno(740);
        }
        return baseRespVo;
    }

}
